package com.gzx.plugin.data.wxjk.sync.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gzx.plugin.data.wxjk.sync.DataConstant;
import com.gzx.plugin.data.wxjk.sync.pojo.RepayData;
import com.gzx.plugin.data.wxjk.sync.service.HksjService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/data/receive/wxjk")
@Slf4j
public class HksjController {

    //维信金科还款数据推送接口（律所提供）
    @Autowired
    private HksjService hksjService;

    @PostMapping(value = "/hksj", produces = {"application/json;charset=UTF-8"}, consumes = {"application/json;charset=UTF-8"})
    public String hksj(@RequestBody String req) {
        log.info("接收维信金科还款数据： {}", req);
        JSONObject reqData = JSON.parseObject(req);
        String data = reqData.getString("repayDataInfo");
        List<RepayData> repayDataList = JSON.parseObject(data, List.class);
        Map<String, String> resultMap = new HashMap<>();//返回给接口调用方组装报文的map，json返回
        try {
            hksjService.save(repayDataList);
            resultMap.put(DataConstant.CODE, DataConstant.ZERO);
            resultMap.put(DataConstant.MESSAGE, "接收成功");
        } catch (Exception e) {
            log.error("save db error:", e);
            resultMap.put(DataConstant.CODE, DataConstant.ONE);
            resultMap.put(DataConstant.MESSAGE, "接收还款数据失败");
        }
        return JSON.toJSONString(resultMap);
    }
}
